package com.lenovo.manufacture.module;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.beardedhen.androidbootstrap.BootstrapLabel;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.lenovo.manufacture.Adapter.recAdapter;
import com.lenovo.manufacture.R;
import com.lenovo.manufacture.net.BaseActivity;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class module9 extends BaseActivity {

    @BindView(R.id.m9_tv1)
    BootstrapLabel m9Tv1;
    @BindView(R.id.m9_tv2)
    BootstrapLabel m9Tv2;
    @BindView(R.id.m9_tv3)
    BootstrapLabel m9Tv3;
    @BindView(R.id.m9_tv4)
    BootstrapLabel m9Tv4;
    @BindView(R.id.chart)
    BarChart chart;
    @BindView(R.id.m9_rv)
    RecyclerView m9Rv;
    @BindView(R.id.m9_t1)
    BootstrapLabel m9T1;
    @BindView(R.id.m9_t2)
    BootstrapLabel m9T2;
    @BindView(R.id.m9_t3)
    BootstrapLabel m9T3;

    List<Map<String, String>> sell;
    List<Map<String, String>> car;
    recAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_module9);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        m9T1.setTag(true);
        m9T2.setTag(true);
        m9T3.setTag(true);
        async(data.getSellOutLog(), v -> async(data.getcartype(), v1 -> {
                    sell = v.getData();
                    car = v1.getData();
                    setTv();
                    setChart();
                    setList();
                })
        );
    }

    private void setList() {
        List<String> list = new ArrayList<>();
        for (Map<String, String> sellOutLog : sell) {
            list.add(car.get(Integer.valueOf(sellOutLog.get("carTypeId"))).get("carTypeName"));
            list.add(decimalFormat.format(Integer.valueOf(sellOutLog.get("gold"))));
            list.add(simpleDateFormat.format(new Date(1000 * Long.valueOf(sellOutLog.get("time")))));
            list.add(sellOutLog.get("num"));
        }
        m9Rv.setLayoutManager(new GridLayoutManager(this, 4));
        adapter = new recAdapter(list);
        m9Rv.setAdapter(adapter);
    }

    @SuppressLint("NewApi")
    private void setChart() {
        //设置数据
        List<BarEntry> barEntries = new ArrayList<>();
        for (Map<String, String> sellOutLog : sell) {
            barEntries.add(new BarEntry(Long.valueOf(sellOutLog.get("time")), Integer.valueOf(sellOutLog.get("num")) * Integer.valueOf(sellOutLog.get("gold"))));
        }
        Collections.sort(barEntries, (o1, o2) -> (int) (o1.getX() - o2.getX()));
        BarDataSet bardataset = new BarDataSet(barEntries, "销售金额");
        bardataset.setColor(Color.parseColor("#1AE61A"));//柱子颜色
        //设置样式
        BarData bardata = new BarData(bardataset);
        bardata.setBarWidth(5000f);//柱子宽度
        chart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);// 设置x轴显示在下方，默认在上方
        chart.getXAxis().setTextSize(1f);
        chart.getXAxis().setDrawGridLines(false);//表格画线
        chart.getXAxis().setValueFormatter(new ValueFormatter() {//显示样式
            @Override
            public String getFormattedValue(float value) {
                return simpleDateFormat.format(new Date(((long) value * 1000)));
            }
        });
        chart.setData(bardata);
        chart.setScaleEnabled(false);
        chart.invalidate();//刷新
    }

    @SuppressLint("NewApi")
    private void setTv() {
        int x = 0, y = 0, z = 0;
        //总金额
        System.out.println(sell.stream().map(stringStringMap -> Integer.valueOf(stringStringMap.get("num")) * Integer.valueOf(stringStringMap.get("gold"))).reduce(0, Integer::sum));
//        int sum = sell.stream()
//                .map(sellOutLog -> sellOutLog.getNum() * sellOutLog.getGold())
//                .reduce(0, Integer::sum);
//        System.out.println(sum);
        for (Map<String, String> sellOutLog : sell) {
            System.out.println(sellOutLog);
            switch (sellOutLog.get("carTypeId")) {
                case "0":
                    x += Integer.valueOf(sellOutLog.get("gold")) * Integer.valueOf(sellOutLog.get("num"));
                    break;
                case "1":
                    y += Integer.valueOf(sellOutLog.get("gold")) * Integer.valueOf(sellOutLog.get("num"));
                    break;
                case "2":
                    z += Integer.valueOf(sellOutLog.get("gold")) * Integer.valueOf(sellOutLog.get("num"));
                    break;
            }
        }
        m9Tv1.setText("汽车总销售额（元)"+'\n'+decimalFormat.format(x + y + z));
        m9Tv2.setText("汽车1总销售额（元)"+'\n'+decimalFormat.format(x));
        m9Tv3.setText("汽车2总销售额（元)"+'\n'+decimalFormat.format(y));
        m9Tv4.setText("汽车3总销售额（元)"+'\n'+decimalFormat.format(z));
    }

    @OnClick({R.id.m9_t1, R.id.m9_t2, R.id.m9_t3})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.m9_t1:
                sort(sell, "gold", m9T1, "价格");
                break;
            case R.id.m9_t2:
                sort(sell, "time", m9T2, "卖出时间");
                break;
            case R.id.m9_t3:
                sort(sell, "num", m9T3, "卖出时间");
                break;
        }
        setList();
    }
}